import Vue from 'vue';
import VueRouter from 'vue-router';
import i18n from '@/i18n';

Vue.use(VueRouter);

const routes=[{
    path: '/', redirect: '/login' //添加一个路由重定向  或者 用alias: '/' 用别名
}, {
    path: '/login',
    component: resolve => require(['@/views/login'], resolve),
    meta: {
        title: i18n.t('router.login_title'),
        // vist_label: ['super_admin', 'admin', 'user']
    }
}, {
    path: '/regedit', 
    component: () => import('@/views/regedit'),
    meta: {title: i18n.t('router.regedit_title')}
}, {
    path: '/resetPassword',
    component: () => import('@/views/resetPassword'),
    meta: {title: i18n.t('router.reset_password_title')}
}, {
    path: '/home',
    name: 'home',
    component: () => import('@/views/home'),
    meta: {
        title: '主页',
        // vist_label: ['super_admin', 'admin', 'user']
    },
    children: [{
        path: 'dashboard',
        alias: '', //用别名 或者 再添加一个路由重定向 或者 path:'' 使用默认子路由
        component: ()=>import('@/views/home/dashboard'),
        meta: {title: '首页看板'}
    }, {
        path: 'user',
        component: ()=>import('@/views/home/userinfo'),
        meta: {group: i18n.t('router.sys_group'), title: i18n.t('router.sys_user_title')}
    }, {
        path: 'role',
        component: ()=>import('@/views/home/roleinfo'),
        meta: {group: i18n.t('router.sys_group'), title: i18n.t('router.sys_role_title')}
    }, {
        path: 'menu',
        component: ()=>import('@/views/home/menuinfo'),
        meta: {group: i18n.t('router.sys_group'), title: i18n.t('router.sys_menu_title')}
    }, {
        path: 'selfinfo',
        component: ()=>import('@/views/home/selfinfo'),
        meta: {group: i18n.t('router.self_group'), title: i18n.t('router.self_info_title')}
    }, {
        path: 'password',
        component: ()=>import('@/views/home/password'),
        meta: {group: i18n.t('router.self_group'), title: i18n.t('router.self_password_title')}
    }]
}, {
    path: '/404',
    component:()=>import('@/views/error/404'),
    meta:{title: i18n.t('router.not_fond_title')}
}];

const router = new VueRouter({routes});

// 捕获同一push的异常
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

const sysname = i18n.t('router.sysname');
// 路由前置导航守卫
router.beforeEach((to,from,next)=>{
    next();
    // 根据路由元信息设置文档标题
    let title = to.meta.title; title = title===undefined?'':title;
    window.document.title = `${sysname}-${title}` || sysname;
});

export default router;